from celery_tasks.main import celery_app
from libs.yuntongxun.sms import CCP
import logging
# from celery_tasks.main import celery_app
#celery -A celery_tasks.main worker -l INFO
logger =logging.getLogger('django')

# name：异步任务别名
@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code):
    """
    发送短信验证码
    :param mobile: 手机号
    :param sms_code: 短信验证码
    :return: None
    """
    try:
        # 调用云通讯短信验证码模块发送短信验证码
        # 云通讯模板要求：参数1为1-4位数字，参数2为有效时间（分钟）
        send_ret = CCP().send_template_sms(mobile, [sms_code, 5], 1)
        if send_ret != 0:
            logger.error('发送短信验证码失败, 手机号: %s, 短信验证码: %s, 发送结果: %s' % (mobile, sms_code, send_ret))
            raise Exception('发送短信验证码失败，错误码: %s' % send_ret)
    except Exception as e:
        logger.error('发送短信验证码失败, 手机号: %s, 短信验证码: %s, 异常信息: %s' % (mobile, sms_code, e))
        raise e